package com.samtech.spring.components;

import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
@Component("loginPageBean")
@Scope("request")
public class LoginPageBean {
	
	 boolean loginError = false;
    private String errorMsg = "none";
    private String lastUser = "";
    private String lastusername="SPRING_SECURITY_LAST_USERNAME";
    private String lastexceptionkey="SPRING_SECURITY_LAST_EXCEPTION";
    private AuthenticationManager authenticationManager;
	public boolean isInited(){
		FacesContext cxt = FacesContext.getCurrentInstance();
		Map<String, String> reqParams = cxt.getExternalContext().getRequestParameterMap();
		boolean e = reqParams.containsKey("LOGIN_ERROR");// request.getParameter(ERROR_PARAMETER_NAME) != null;
		if(e && reqParams.get("LOGIN_ERROR")!=null)loginError=true;
		Object session = cxt.getExternalContext().getSession(false);
		 if (session != null) {
			 AuthenticationException ex = (AuthenticationException) ((HttpSession)session).getAttribute(lastexceptionkey);
			 if(ex!=null){
				 ex.printStackTrace();
				 if(ex.getMessage()!=null)errorMsg=ex.getMessage();
			 }
			 String s=(String)((HttpSession)session).getAttribute(lastusername);
			 if(s!=null)
			 lastUser=s;
	    }
		return false;
	}
	public LoginPageBean() {
		
	}
	
	public boolean isError(){
		return loginError;
	}
	public String  getErrorMessage(){
		return errorMsg;
	}
	public String getLastUserName(){
		return lastUser;
	}
	
	/*
	 * 
        boolean loginError = request.getParameter(ERROR_PARAMETER_NAME) != null;
        String errorMsg = "none";
        String lastUser = "";

        if (loginError) {
            HttpSession session = request.getSession(false);

            if(session != null) {
                lastUser = (String) session.getAttribute(UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY);
                AuthenticationException ex = (AuthenticationException) session.getAttribute(AbstractAuthenticationProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);
                errorMsg = ex != null ? ex.getMessage() : "none";
                if (lastUser == null) {
                    lastUser = "";
                }
            }
        }

        StringBuilder sb = new StringBuilder();

        sb.append("<html><head><title>Login Page</title></head>");

        if (formLoginEnabled) {
            sb.append("<body onload='document.f.").append(usernameParameter).append(".focus();'>\n");
        }

        if (loginError) {
            sb.append("<p><font color='red'>Your login attempt was not successful, try again.<br/><br/>Reason: ");
            sb.append(errorMsg);
            sb.append("</font></p>");
        }

        if (formLoginEnabled) {
            sb.append("<h3>Login with Username and Password</h3>");
            sb.append("<form name='f' action='").append(request.getContextPath()).append(authenticationUrl).append("' method='POST'>\n");
            sb.append(" <table>\n");
            sb.append("    <tr><td>User:</td><td><input type='text' name='");
            sb.append(usernameParameter).append("' value='").append(lastUser).append("'></td></tr>\n");
            sb.append("    <tr><td>Password:</td><td><input type='password' name='").append(passwordParameter).append("'/></td></tr>\n");

            if (rememberMeParameter != null) {
                sb.append("    <tr><td><input type='checkbox' name='").append(rememberMeParameter).append("'/></td><td>Remember me on this computer.</td></tr>\n");
            }

            sb.append("    <tr><td colspan='2'><input name=\"submit\" type=\"submit\"/></td></tr>\n");
            sb.append("    <tr><td colspan='2'><input name=\"reset\" type=\"reset\"/></td></tr>\n");
            sb.append("  </table>\n");
            sb.append("</form>");
        }

        if(openIdEnabled) {
            sb.append("<h3>Login with OpenID Identity</h3>");
            sb.append("<form name='oidf' action='").append(request.getContextPath()).append(openIDauthenticationUrl).append("' method='POST'>\n");
            sb.append(" <table>\n");
            sb.append("    <tr><td>Identity:</td><td><input type='text' name='");
            sb.append(openIDusernameParameter).append("'/></td></tr>\n");

            if (openIDrememberMeParameter != null) {
                sb.append("    <tr><td><input type='checkbox' name='").append(openIDrememberMeParameter).append("'></td><td>Remember me on this computer.</td></tr>\n");
            }

            sb.append("    <tr><td colspan='2'><input name=\"submit\" type=\"submit\"/></td></tr>\n");
            sb.append("    <tr><td colspan='2'><input name=\"reset\" type=\"reset\"/></td></tr>\n");
            sb.append("  </table>\n");
            sb.append("</form>");
        }

        sb.append("</body></html>");

        return sb.toString();
    
	 */
}
